Subject: Re: PIPEing
From: MenTaT - !Productions <[email-masked]>
To: [email-masked] (M D Cox)
Cc: [email-masked]
Date: Sat, 23 Jul 1994 15:21:19 -0400 (EDT)

> 
> Has anyone ever written a program to accept STDIN or piping?  For
> instance, if I wrote a program called WC that would count the number of words
> and lines in a text file, would I be able to:
> 
> type textfile.txt > WC
> 
> or
> 
> type textfile.txt | WC

I'm not sure how the shell handles pipes - the easiest way to test it 
would be to try it!
 
> I know i can do: WC textfile.txt, but then could I do:
> 
> WC textfile.txt > result.txt

Only if WC directed its output to the current shell window, which under 
AMOS is hard to do.  You could be sneaky, and use a different 
metacharacter, maybe @ or something else that isn't used, and get the 
AMOS program to redirect it, but it's not a very good solution...  For 
this kind of thing you're better off using C.

> with result.txt being created and containing the output of WC?
> 
> If anyone is familiar with programs like AWK or GREP, help me out if you
> can, please!  :)

Here's a nice awk program...  (Dunno if it works in GNU awk, but it works 
in System V...)

awk '
FILENAME != prevfile {    # new file
    NR = 1                # reset line number
    prevfile = FILENAME
}
NF > 0 {
    if ($1 == lastword)
        printf "double %s, file %s, line %d\n",$1,FILENAME,NR
    for (i = 2; i <= NF; i++)
        if ($i == $(i-1))
            printf "double %s, file %s, line %d\n",$i,FILENAME,NR
    if (NF > 0)
        lastword = $NF
}' $*

It looks for pairs of identical adjacent words...  Kinda nifty. :)  (And 
you can pipe it.  Not that this helps you at all. :)

I don't think AMOS was really ever designed for running from a shell, so 
while there probably IS a way of piping and redirecting input and output, 
C would be a lot easier.  (What am I saying?  C EASY? :)

Any awk/grep questions, just ask! :)  (And here's something nice - search 
for users with no password an a *NIX machine :

grep '^[^:]*::' /etc/passwd

Not that I condone this sort of activity... :)